package com.fengyunhe.common;

import com.fengyunhe.common.utils.HttpRequestVisit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * UserAgent 解析处理拦截器
 *
 * @author Administrator
 */
public class UserAgentParseInterceptor implements HandlerInterceptor {


    Logger log = LoggerFactory.getLogger(UserAgentParseInterceptor.class);

    @Override
    public void afterCompletion(HttpServletRequest arg0,
                                HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
//        清除visit
        RequestThread.setVisit(null);
    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
                           Object arg2, ModelAndView arg3)
            throws Exception {

    }

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object arg2) throws Exception {
//        设置visit
        RequestThread.setVisit(new HttpRequestVisit(request));
        return true;
    }
}
